package com.hsnr.temperaturkontrolle;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class WebViewActivity extends Activity {

	private Button button1, button2;
	private int count = 0;
	private WebView webview;

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);

		final String[] links = new String[4];

		links[0] = "http://www08.mg.hs-niederrhein.de/studenten/studentwi/bwi50205/p/Hl867923/entwurf1.php?gf=1 ";
		links[1] = "http://www08.mg.hs-niederrhein.de/studenten/studentwi/bwi50205/p/Hl867923/entwurf2.php";
		links[2] = "http://www08.mg.hs-niederrhein.de/studenten/studentwi/bwi50205/p/Hl867923/createpng.php?gf=1";
		links[3] = "http://www08.mg.hs-niederrhein.de/studenten/studentwi/bwi50205/p/Hl867923/createpng2forall.php";

		if (!isNetworkAvailable()) {
			Toast.makeText(this, "Kein Internet!", Toast.LENGTH_LONG).show();
			return;
		}

		// WebView webview = new WebView(this);
		webview = (WebView) findViewById(R.id.webview);
		webview.getSettings().setBuiltInZoomControls(true);
		webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
		webview.loadUrl(links[0]);

		button1 = (Button) findViewById(R.id.startbutton);
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (count == 0) {

					count = (links.length - 1);
				} else {

					count--;
				}

				webview.loadUrl(links[count]);

			}
		});

		button2 = (Button) findViewById(R.id.button2);
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (count == links.length - 1) {

					count = 0;
				} else {

					count++;
				}

				webview.loadUrl(links[count]);

			}
		});
	}

	private boolean isNetworkAvailable() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

		return networkInfo != null && networkInfo.isConnected();
	}

	public void onBackpressed() {

		finish();
	}

}
